package com.lidroid.xutils;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import com.lidroid.xutils.view.annotation.Select;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.event.ViewCommonEventListener;

import java.lang.reflect.Field;

public class ViewUtils {

    private ViewUtils() {
    }

    public static boolean isEmpty( CharSequence str) {
        return str == null || str.length() == 0;
    }

    public static void inject(AbilitySlice abilitySlice) {
        injectActivity(abilitySlice);
    }

    public static void injectComponent(Component view) {
        injectView(view);
    }

    private static void injectActivity(AbilitySlice abilitySlice) {
        Field[] fields = abilitySlice.getClass().getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                ViewInject viewInject = field.getAnnotation(ViewInject.class);
                if (viewInject != null) {
                    int viewId = viewInject.id();
                    try {
                        field.setAccessible(true);
                        field.set(abilitySlice, abilitySlice.findComponentById(viewId));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    String clickMethod = viewInject.click();
                    if (!isEmpty(clickMethod))
                        setViewClickListener(abilitySlice, field, clickMethod);

                    String longClickMethod = viewInject.longClick();
                    if (!isEmpty(longClickMethod))
                        setViewLongClickListener(abilitySlice, field, longClickMethod);

                    String itemClickMethod = viewInject.itemClick();
                    if (!isEmpty(itemClickMethod))
                        setItemClickListener(abilitySlice, field, itemClickMethod);

                    String itemLongClickMethod = viewInject.itemLongClick();
                    if (!isEmpty(itemLongClickMethod))
                        setItemLongClickListener(abilitySlice, field, itemLongClickMethod);

                    Select select = viewInject.select();
                    if (!isEmpty(select.selected()))
                        setViewSelectListener(abilitySlice, field, select.selected(), select.noSelected());

                }
            }
        }
    }

    private static void injectView(Component view) {
        Field[] fields = view.getClass().getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                ViewInject viewInject = field.getAnnotation(ViewInject.class);
                if (viewInject != null) {
                    int viewId = viewInject.id();
                    try {
                        field.setAccessible(true);
                        field.set(view, view.findComponentById(viewId));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    String clickMethod = viewInject.click();
                    if (!isEmpty(clickMethod))
                        setViewClickListener(view, field, clickMethod);

                    String longClickMethod = viewInject.longClick();
                    if (!isEmpty(longClickMethod))
                        setViewLongClickListener(view, field, longClickMethod);

                    String itemClickMethod = viewInject.itemClick();
                    if (!isEmpty(itemClickMethod))
                        setItemClickListener(view, field, itemClickMethod);

                    String itemLongClickMethod = viewInject.itemLongClick();
                    if (!isEmpty(itemLongClickMethod))
                        setItemLongClickListener(view, field, itemLongClickMethod);

                    Select select = viewInject.select();
                    if (!isEmpty(select.selected()))
                        setViewSelectListener(view, field, select.selected(), select.noSelected());

                }
            }
        }
    }


    private static void setViewClickListener(Object target, Field field, String clickMethod) {
        try {
            Object obj = field.get(target);
            if (obj instanceof Component) {
                ((Component) obj).setClickedListener(new ViewCommonEventListener(target).click(clickMethod));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setViewLongClickListener(Object target, Field field, String clickMethod) {
        try {
            Object obj = field.get(target);
            if (obj instanceof Component) {
                ((Component) obj).setLongClickedListener(new ViewCommonEventListener(target).longClick(clickMethod));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setItemClickListener(Object target, Field field, String itemClickMethod) {
        try {
            Object obj = field.get(target);
            if (obj instanceof ListContainer) {
                ((ListContainer) obj).setItemClickedListener(new ViewCommonEventListener(target).itemClick(itemClickMethod));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setItemLongClickListener(Object target, Field field, String itemClickMethod) {
        try {
            Object obj = field.get(target);
            if (obj instanceof ListContainer) {
                ((ListContainer) obj).setItemLongClickedListener(new ViewCommonEventListener(target).itemLongClick(itemClickMethod));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setViewSelectListener(Object target, Field field, String select, String noSelect) {
        try {
            Object obj = field.get(target);
            if (obj instanceof ListContainer) {
                ((ListContainer) obj).setItemSelectedListener(new ViewCommonEventListener(target).select(select).noSelect(noSelect));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
